package com.lianbang.pharmacy.vm

import cn.ubest.mvvmcore.base.BaseListViewModel
import cn.ubest.mvvmcore.base.BaseViewModel
import cn.ubest.mvvmcore.interfaces.RequestHandler1Impl
import cn.ubest.mvvmcore.utils.ActivityManager
import com.lianbang.pharmacy.bean.UserBean
import com.lianbang.pharmacy.model.MainModel
import com.lianbang.pharmacy.model.UserModel
import com.lianbang.pharmacy.module.main.MainActivity
import com.lianbang.pharmacy.module.main.StartActivity
import com.lianbang.pharmacy.util.LoginUtils

/**
 * Created by maoqi on 2020/11/21.
 */
class MainVM : BaseListViewModel<MainModel>() {
    private val mUserModel by lazy {
        UserModel()
    }

    override fun loadData(isInit: Boolean, vararg arg: Any) {
        setData(mutableListOf("", "", "", "", "", "", "", "", "", "", "", "", ""))
    }

    fun getUser() {
        requestWithLife(object : RequestHandler1Impl<UserBean>(this) {
            override suspend fun request1(): UserBean {
                return mUserModel.getUser().resolveWrapper()
            }

            override fun onSuccess(t: UserBean) {
                LoginUtils.loginSucc(t)
                val topActivity = ActivityManager.getInstance().topActivity
                if (topActivity is StartActivity) {
                    MainActivity.start(topActivity, MainActivity.TAB_HOME)
                    topActivity.finish()
                }
            }
        })
    }
}